<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>聊天窗口</title>
</head>
<body>
<input id="user"><button onclick="conn()">连接</button><br>
<textarea id="msgs"></textarea><br>
<input id="msg"><button onclick="send()">发送</button>

<script>
    var websocket;
    function conn(){
        websocket = new WebSocket("ws://localhost/websocket/" + user.value);
        //连接发生错误的回调方法
        websocket.onerror = function(){
            alert("连接错误!");
        };
        //连接成功建立的回调方法
        websocket.onopen = function(event){
            alert("连接成功!");
        }
        //接收到消息的回调方法
        websocket.onmessage = function(event){
            console.info(event);
            msgs.innerHTML = msgs.innerHTML + event.data + "\n";
        }
        //连接关闭的回调方法
        websocket.onclose = function(){
            alert("连接关闭!");
        }
    }

    //发送消息
    function send(){
        websocket.send( msg.value);
    }






/*
    // 创建websocket对象
    var websocket = new WebSocket("ws://localhost:8084/websocket");
    //连接发生错误的回调方法
    websocket.onerror = function(){ };
    //连接成功建立的回调方法
    websocket.onopen = function(event){ }
    //接收到消息的回调方法
    websocket.onmessage = function(event){ }
    //连接关闭的回调方法
    websocket.onclose = function(){ }
    //关闭连接
    function closeWebSocket(){ websocket.close(); }
    //发送消息
    function send(msg){ websocket.send(msg); }*/



</script>

</body>
</html>